﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using TA.JSON;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Repeater value.
	/// </summary>
	public class RepeaterValue : IJsonCompatible, ICloneable//, IRepeaterComponent
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public RepeaterValue()
		{
			Entries = new PCRepeaterEntries();
		}
		/// <summary>
		/// Copy ctor.
		/// </summary>
		/// <param name="v"></param>
		public RepeaterValue(RepeaterValue v)
		{
			this.Entries = (IRepeaterEntries)v.Entries.Clone();
			this.FirstIndex = v.FirstIndex;
			this.NumEntries = v.NumEntries;
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Collection of entries.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public IRepeaterEntries Entries
		{
			get
			{
				return this._entries;
			}
			set
			{
				this._entries = value;
				this._entries.Owner = this;
			}
		}
		/// <summary>
		/// Gets or sets the number of entries artificially for paging purposes (in scenarios where not the whole result set is returned).
		/// </summary>
		public int NumEntries
		{
			get
			{
				return Math.Max(this.Entries.Count, this._numEntries);
			}
			set
			{
				this._numEntries = value;
				this.FirstIndex = Math.Min(this.FirstIndex, value - this.Entries.Count);
			}
		}
		/// <summary>
		/// Gets or sets the index of the first entry artificially for paging purposes (in scenarios where not the whole result set is returned).
		/// </summary>
		public int FirstIndex { get; set; }
		#endregion
		#region EVENTS
		///// <summary>
		///// Raised when an entry needs to be serialized.
		///// </summary>
		//public event EventHandler<SerializeEventArgs> SerializeEntry;
		///// <summary>
		///// Raised when an entry needs to be deserialized.
		///// </summary>
		//public event EventHandler<DeserializeEventArgs> DeserializeEntry;
		#endregion
		#region METHODS
		/// <summary>
		/// Creates a new object that is a copy of the current instance.
		/// </summary>
		/// <returns></returns>
		public object Clone()
		{
			return new RepeaterValue(this);
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		internal Repeater _owner;
		int _numEntries;
		IRepeaterEntries _entries;
		#endregion
		#region METHODS
		#region IJsonSerializable Members
		/// <summary>
		/// IsDirty.
		/// </summary>
		public bool IsDirty
		{
			get
			{
				return true;
			}
		}
		/// <summary>
		/// Serialize.
		/// </summary>
		/// <param name="jw"></param>
		public void Serialize(JsonWriter jw)
		{
			this.SerializeObject(jw);
		}
		/// <summary>
		/// SerializeContents,
		/// </summary>
		/// <param name="jw"></param>
		public void SerializeContents(JsonWriter jw)
		{
			jw.PropertyName("N");
			jw.Value(this.NumEntries);

			jw.PropertyName("F");
			jw.Value(this.FirstIndex);

			jw.PropertyName("E");
			jw.Value(this.Entries);
		}

		#endregion
		#region IJsonDeserializable Members
		/// <summary>
		/// Deserialize.
		/// </summary>
		/// <param name="jr"></param>
		public void Deserialize(IJsonReader jr)
		{
			this.DeserializeObject(jr);
		}
		/// <summary>
		/// DeserializeContent.
		/// </summary>
		/// <param name="jr"></param>
		/// <param name="propShortName"></param>
		public void DeserializeContent(IJsonReader jr, string propShortName)
		{
			switch (propShortName)
			{
				case "N":
					this.NumEntries = (int)(long)jr.Value; break;
				case "F":
					this.FirstIndex = (int)(long)jr.Value; break;
				case "E":
					this.Entries.Deserialize(jr); break;
			}
		}

		#endregion
		#endregion
		#endregion

		//#region IRepeaterComponent Members

		//public IRepeaterComponent Owner
		//{
		//  get
		//  {
		//    throw new NotImplementedException();
		//  }
		//  set
		//  {
		//    throw new NotImplementedException();
		//  }
		//}

		//public Repeater Repeater
		//{
		//  get { return  }
		//}

		//#endregion
	}
}